{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "如果想要应用自定义的函数，或者把其他库中的函数应用到 Pandas 对象中，有以下三种方法：\n",
    "- 1) 操作整个 DataFrame 的函数：pipe()\n",
    "- 2) 操作行或者列的函数：apply()\n",
    "- 3) 操作单一元素的函数：applymap()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "    index01   index02   index03\n",
      "0  0.733903 -0.781854  1.588798\n",
      "1  1.838088  0.732198  1.628097\n",
      "2 -1.399200 -0.627474  0.448264\n",
      "3  4.095283  0.113130 -0.485356\n",
      "     index01    index02    index03\n",
      "0  10.733903   9.218146  11.588798\n",
      "1  11.838088  10.732198  11.628097\n",
      "2   8.600800   9.372526  10.448264\n",
      "3  14.095283  10.113130   9.514644\n",
      "    index01   index02   index03\n",
      "0  0.733903 -0.781854  1.588798\n",
      "1  1.838088  0.732198  1.628097\n",
      "2 -1.399200 -0.627474  0.448264\n",
      "3  4.095283  0.113130 -0.485356\n"
     ]
    }
   ],
   "source": [
    "# 遍历所有元素操作\n",
    "def add(num1,num2):\n",
    "   return num1 + num2\n",
    "#操作DataFrame\n",
    "df = pd.DataFrame(np.random.randn(4,3),columns=['index01','index02','index03'])\n",
    "#相加前\n",
    "print(df)\n",
    "#相加后\n",
    "print(df.pipe(add,10))\n",
    "print(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "index01    1.317019\n",
      "index02   -0.141000\n",
      "index03    0.794951\n",
      "dtype: float64\n",
      "----------------------------------------------------------------\n",
      "0    0.513616\n",
      "1    1.399461\n",
      "2   -0.526137\n",
      "3    1.241019\n",
      "dtype: float64\n",
      "----------------------------------------------------------------\n",
      "0    2.370651\n",
      "1    1.105890\n",
      "2    1.847464\n",
      "3    4.580639\n",
      "dtype: float64\n",
      "----------------------------------------------------------------\n",
      "       index01     index02      index03\n",
      "0   733.903426 -781.853776  1588.797561\n",
      "1  1838.087996  732.197717  1628.096859\n",
      "2 -1399.200041 -627.474041   448.264138\n",
      "3  4095.283366  113.129984  -485.355768\n"
     ]
    }
   ],
   "source": [
    "# 操作行或列 apply()\n",
    "print(df.apply(np.mean,axis=0))\n",
    "print(\"----------------------------------------------------------------\")\n",
    "print(df.apply(np.mean,axis=1))\n",
    "print(\"----------------------------------------------------------------\")\n",
    "print(df.apply(lambda x: x.max()- x.min(),axis=1))\n",
    "print(\"----------------------------------------------------------------\")\n",
    "print(df.apply(lambda x: x * 1000))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "     index01   index02    index03\n",
      "0   7.339034 -7.818538  15.887976\n",
      "1  18.380880  7.321977  16.280969\n",
      "2 -13.992000 -6.274740   4.482641\n",
      "3  40.952834  1.131300  -4.853558\n",
      "----------------------------------------------------------------\n",
      "index01    1.317019\n",
      "index02   -0.141000\n",
      "index03    0.794951\n",
      "dtype: float64\n"
     ]
    }
   ],
   "source": [
    "# 操作行或列 applymap()\n",
    "print(df.applymap(lambda x:x*10))\n",
    "print(\"----------------------------------------------------------------\")\n",
    "print(df.apply(np.mean))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
